package com.lbs.service.Impl;

import com.lbs.dao.Impl.StuDaoImpl;
import com.lbs.dao.StuDao;
import com.lbs.entity.Stu;
import com.lbs.service.StuService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2021/5/18 14:03
 * @description
 **/
public class StuServiceImpl implements StuService {

    StuDao stuDao = new StuDaoImpl();

    @Override
    public boolean addStu(Stu stu) {
        int i = stuDao.addStu(stu);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    // 条件查询
    @Override
    public List<Stu> searchStu(String name, String gender) {

        String sql = "select * from stu where 1=1";

        List<String> list = new ArrayList<>();

        if (name != null && name != "") {
            sql = sql + " and name like ?";
            list.add("%"+name+"%");
        }
        if (gender != null && (gender.equals("男")||gender.equals("女"))) {
            sql = sql + " and gender = ?";
            list.add(gender);
        }

        return stuDao.findStuByNameAndGender(sql, list.toArray());
    }

    @Override
    public boolean delStu(String ids) {
        if (ids.length()>0&&ids!=null){
            int i = stuDao.delStuById(ids);
            if (i>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updateStu(Stu stu) {
        int i = stuDao.updateStuById(stu);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Stu selectStuById(String id) {
        return stuDao.selectStuById(id);
    }
}
